<?php


namespace King\Service;


use King\Entity\Poker\Poker;
use King\Entity\User\User;
use phpDocumentor\Reflection\Types\Integer;

class PokerPlayService
{
    /** @var User */
    public $user;
    public static $discardList = [];
    public static $prevCardList = [];
    public static $prevPokerRule;
    public static $nextPlay = 0;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function outCard(array $outCard): PokerPlayService
    {
        $ruleService = (new PokerRuleService)->make($outCard);
        if (empty(self::$prevPokerRule)) {
            $compare = true;
            self::$prevPokerRule = $ruleService;
            self::$prevCardList = $outCard;
        } else {
            $compare = (new PokerCompareService(self::$prevPokerRule, $ruleService))->check();
            var_dump($compare);
        }
        /**
         * @var integer $index
         * @var Poker $item
         */
        foreach ($this->user->cardList as $index => $item) {
            /** @var Poker $cardObj */
            foreach ($outCard as $cardObj) {
                if ($cardObj->name == $item->name && $cardObj->color == $item->color) {
                    unset($this->user->cardList[$index]);
                    array_push(self::$discardList, $cardObj);
                }
            }
        }

        $len = count($this->user->cardList);
        if ($len == 0) {
            throw new \Exception($this->user->name);
        }
        return $this;
    }

    /**
     * 冒泡排序
     * @return $this
     */
    public function numberSort(): PokerPlayService
    {
        $list = json_decode(json_encode($this->user->cardList), true);
        $this->user->cardList = $this->arrayToPoker((new PokerSortService)->numberSort($list));
        return $this;
    }

    public function arrayToPoker(array $list): array
    {
        $pokerList = [];
        foreach ($list as $index => $item) {
            $_item = new Poker();
            $_item->name = $item['name'];
            $_item->color = $item['color'];
            array_push($pokerList, $_item);
        }
        return $pokerList;
    }


}